<?php
namespace App\Http\Controllers\Admin;

use App\AppChannel;
use App\AppWhiteList;
use App\Http\Controllers\Controller;
use App\UidAppidRel;
use App\UserApp;
use Illuminate\Http\Request;

class WhiteListController extends Controller
{
    /**
     * @Desc: 白名单列表
     * @Author: hank <maokeyang1031@gmail.com>
     * @param Request $request
     * @return \Illuminate\View\View
     */
    public function whiteList(Request $request)
    {
        $appId = $request->input('app_id');
        $channelId = $request->input('channel_id',0 );

        $this->preFilter(new UidAppidRel(), $appId);

        $appWhiteModel = new AppWhiteList();
        if (!empty($channelId)) {
            $appWhiteModel = $appWhiteModel->where("channel_id", $channelId);
        }

        $list = $appWhiteModel->where("app_id", $appId)
                            ->orderBy("id", "desc")->paginate(10);


        foreach ($list as &$item) {
            $item->channel_name = AppChannel::where("id", $item->channel_id)->value("name");
        }

        $channelRes = AppChannel::where("app_id", $appId)->get(["id", "name"]);
        return view('admin.white_list.index', ['channelRes' => $channelRes, 'list' => $list, 'appId' => $appId, 'channelId' => $channelId]);
    }


    /**
     * @Desc: 添加白名单
     * @Author: hank <maokeyang1031@gmail.com>
     * @param Request $request
     * @return \Illuminate\View\View
     */
    public function whiteAddView(Request $request)
    {
        $appId = $request->get("app_id", 0);
        if (empty($appId)) {
            return abort(403, "请求参数错误");
        }

        $channelRes = AppChannel::where("app_id", $appId)->get(["id", "name"]);


        return view('admin.white_list.add', ['appId' => $appId, 'channelRes' => $channelRes]);
    }

    /**
     * 添加白名单
     *
     * @param \Illuminate\Http\Request $request
     *
     * @return mixed
     */
    public function whiteAdd(Request $request)
    {
        $data   = $request->post();
        $channel = new AppWhiteList();

        $channel->fill($data)->save();
        return $this->json(200, '添加成功');
    }

    /**
     * 修改白名单信息
     *
     * @Author: hank <maokeyang1031@gmail.com>
     * @param Request $request
     * @param $id
     * @return \Illuminate\View\View
     */
    public function whiteUpdateView(Request $request, $id)
    {

        $white = AppWhiteList::findOrFail($id);

        $channelRes = AppChannel::where("app_id", $white->app_id)->get(["id", "name"]);

        return view('admin.white_list.update', ['white' => $white, 'channelRes' => $channelRes]);
    }

    /**
     * 白名单更新
     *
     * @param Request $request
     * @param $id
     * @return mixed|void
     */
    public function whiteUpdate(Request $request, $id)
    {
        $white = AppWhiteList::findOrFail($id);

//        if (!session('admin')->hasSuperRole()) {
//            return abort(403, "非法请求");
//        }

        $data   = $request->post();
        $white->fill($data);
        $white->save();
        return $this->json(200, '修改成功');
    }

    /**
     * 删除白名单
     *
     * @Author: hank <maokeyang1031@gmail.com>
     * @param $id
     * @return mixed
     */
    public function whiteDel($id)
    {
        $referResult = AppWhiteList::where("id", $id)->first();
        if (empty($referResult)) {
            return abort("404", "参数错误");
        }
//        if (!session('admin')->hasSuperRole()) {
//            return abort(403, "非法请求");
//        }
        AppWhiteList::findOrFail($id)->delete();
        return $this->json(200, '删除成功');
    }
}
